package com.lmiot.floatviewsdklibrary.View;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Point;
import android.os.Vibrator;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.WindowManager;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.OvershootInterpolator;
import com.lmiot.floatviewsdklibrary.SDK.YYFloatButton;
import com.lmiot.floatviewsdklibrary.StateBarUtil;

public class FloatView extends View {
    private static final int[] INNER_CIRCLE_COLOR_RANGE1 = {-1307738381, -1307738381, -452100365, -452100365};
    private static final int[] INNER_CIRCLE_COLOR_RANGE2 = {-1293156301, -1293156301, -437518285, -437518285};
    private static final int[] INNER_CIRCLE_COLOR_RANGE3 = {-1291859456, -1291859456, -436221440, -436221440};
    private static final int[] INNER_CIRCLE_COLOR_RANGE4 = {-1305159680, -1305159680, -449521664, -449521664};
    private static final int[] INNER_CIRCLE_COLOR_RANGE5 = {-1308582511, -1308582511, -452944495, -452944495};
    private static final int[] INNER_CIRCLE_COLOR_RANGE6 = {-1307426645, -1307426645, -451788629, -451788629};
    private static final int[] INNER_CIRCLE_COLOR_RANGE7 = {-1304882001, -1304882001, -449243985, -449243985};
    private static final int[] INNER_CIRCLE_COLOR_RANGE8 = {-1295201076, -1295201076, -439563060, -439563060};
    private static final int[] INNER_CIRCLE_COLOR_RANGE9 = {-1304112458, -1304112458, -448474442, -448474442};
    private static final float[] INNER_CIRCLE_POSITION_RANGE = {0.0f, 0.05f, 0.75f, 1.0f};
    private static final long LONG_CLICK_LIMIT = 300;
    private static final int[] OUT_CIRCLE_COLOR_RANGE2 = {15466547, 15466547, 451674163, 1659633715, 2129395763};
    private static final int[] OUT_CIRCLE_COLOR_RANGE3 = {16763392, 16763392, 452971008, 1660930560, 2130692608};
    private static final int[] OUT_CIRCLE_COLOR_RANGE4 = {3463168, 3463168, 439670784, 1647630336, 2117392384};
    private static final int[] OUT_CIRCLE_COLOR_RANGE5 = {40337, 40337, 436247953, 1644207505, 2113969553};
    private static final int[] OUT_CIRCLE_COLOR_RANGE6 = {1196203, 1196203, 437403819, 1645363371, 2115125419};
    private static final int[] OUT_CIRCLE_COLOR_RANGE7 = {3740847, 3740847, 439948463, 1647908015, 2117670063};
    private static final int[] OUT_CIRCLE_COLOR_RANGE8 = {13421772, 13421772, 449629388, 1657588940, 2127350988};
    private static final int[] OUT_CIRCLE_COLOR_RANGE9 = {4510390, 4510390, 440718006, 1648677558, 2118439606};
    private static final float[] OUT_CIRCLE_POSITION_RANGE = {0.0f, 0.45f, 0.6f, 0.9f, 1.0f};
    private static int statusBarHeight;
    final int[] OUT_CIRCLE_COLOR_RANGE1;
    private Point center;
    private boolean isBeingDrag;
    private ValueAnimator mClickAnimator;
    private Context mContext;
    private int mDistance;
    private int mDownMotionX;
    private int mDownMotionY;
    private float mDownThreshold;
    private ValueAnimator mDragAnimator;
    private InnerBall mInnerBall;
    private float mInnerRadiusRate;
    private boolean mIslongPress;
    private long mLastDownTime;
    private float mMaxMoveDistance;
    private float mMotionDownX;
    private float mMotionDownY;
    private Mode mNowmode;
    public OnLocationChangeListener mOnLocationChangeListener;
    private OutterBall mOutterBall;
    private float mOutterRadiusRate;
    private long[] mPattern;
    private int mScaleTouchSlop;
    private float mUpThreshold;
    private Vibrator mVibrator;
    private float xInScreen;
    private float xInView;
    private float yInScreen;
    private float yInView;

    public enum Mode {
        CLICK,
        LEFT,
        UP,
        RIGHT,
        DOWN,
        NONE
    }

    public interface OnLocationChangeListener {
        void result(float f, float f2, float f3, float f4);
    }

    public void setOnLocationChangeListener(OnLocationChangeListener onLocationChangeListener) {
        this.mOnLocationChangeListener = onLocationChangeListener;
    }

    public FloatView(Context context) {
        this(context, null);
    }

    public FloatView(Context context, AttributeSet attributeSet) {
        this(context, attributeSet, 0);
    }

    public FloatView(Context context, AttributeSet attributeSet, int i) {
        super(context, attributeSet, i);
        this.OUT_CIRCLE_COLOR_RANGE1 = new int[]{884467, 884467, 437092083, 1645051635, 2114813683};
        this.mUpThreshold = 0.99f;
        this.mDownThreshold = 0.0f;
        this.mInnerRadiusRate = 0.4f;
        this.mOutterRadiusRate = 0.9f;
        this.center = new Point();
        this.mPattern = new long[]{0, 50};
        this.mContext = context;
        this.mScaleTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
        this.mVibrator = (Vibrator) getContext().getSystemService("vibrator");
    }

    /* access modifiers changed from: protected */
    public void onSizeChanged(int i, int i2, int i3, int i4) {
        super.onSizeChanged(i, i2, i3, i4);
        this.center.x = ((getMeasuredWidth() + getPaddingLeft()) - getPaddingRight()) / 2;
        this.center.y = ((getMeasuredHeight() + getPaddingTop()) - getPaddingBottom()) / 2;
        int min = Math.min(getMeasuredWidth(), getMeasuredHeight()) / 2;
        this.mInnerBall = new InnerBall(this.center.x, this.center.y, min, this.mInnerRadiusRate);
        this.mOutterBall = new OutterBall(this.center.x, this.center.y, min, this.mOutterRadiusRate);
        int floatViewColor = YYFloatButton.getFloatViewColor(this.mContext);
        Log.d("FloatView", "floatViewColor:" + floatViewColor);
        switch (floatViewColor) {
            case 1:
                this.mInnerBall.setColorsAndPosition(INNER_CIRCLE_COLOR_RANGE1, INNER_CIRCLE_POSITION_RANGE);
                this.mOutterBall.setColorsAndPosition(this.OUT_CIRCLE_COLOR_RANGE1, OUT_CIRCLE_POSITION_RANGE);
                break;
            case 2:
                this.mInnerBall.setColorsAndPosition(INNER_CIRCLE_COLOR_RANGE2, INNER_CIRCLE_POSITION_RANGE);
                this.mOutterBall.setColorsAndPosition(OUT_CIRCLE_COLOR_RANGE2, OUT_CIRCLE_POSITION_RANGE);
                break;
            case 3:
                this.mInnerBall.setColorsAndPosition(INNER_CIRCLE_COLOR_RANGE3, INNER_CIRCLE_POSITION_RANGE);
                this.mOutterBall.setColorsAndPosition(OUT_CIRCLE_COLOR_RANGE3, OUT_CIRCLE_POSITION_RANGE);
                break;
            case 4:
                this.mInnerBall.setColorsAndPosition(INNER_CIRCLE_COLOR_RANGE4, INNER_CIRCLE_POSITION_RANGE);
                this.mOutterBall.setColorsAndPosition(OUT_CIRCLE_COLOR_RANGE4, OUT_CIRCLE_POSITION_RANGE);
                break;
            case 5:
                this.mInnerBall.setColorsAndPosition(INNER_CIRCLE_COLOR_RANGE5, INNER_CIRCLE_POSITION_RANGE);
                this.mOutterBall.setColorsAndPosition(OUT_CIRCLE_COLOR_RANGE5, OUT_CIRCLE_POSITION_RANGE);
                break;
            case 6:
                this.mInnerBall.setColorsAndPosition(INNER_CIRCLE_COLOR_RANGE6, INNER_CIRCLE_POSITION_RANGE);
                this.mOutterBall.setColorsAndPosition(OUT_CIRCLE_COLOR_RANGE6, OUT_CIRCLE_POSITION_RANGE);
                break;
            case 7:
                this.mInnerBall.setColorsAndPosition(INNER_CIRCLE_COLOR_RANGE7, INNER_CIRCLE_POSITION_RANGE);
                this.mOutterBall.setColorsAndPosition(OUT_CIRCLE_COLOR_RANGE7, OUT_CIRCLE_POSITION_RANGE);
                break;
            case 8:
                this.mInnerBall.setColorsAndPosition(INNER_CIRCLE_COLOR_RANGE8, INNER_CIRCLE_POSITION_RANGE);
                this.mOutterBall.setColorsAndPosition(OUT_CIRCLE_COLOR_RANGE8, OUT_CIRCLE_POSITION_RANGE);
                break;
            default:
                this.mInnerBall.setColorsAndPosition(INNER_CIRCLE_COLOR_RANGE9, INNER_CIRCLE_POSITION_RANGE);
                this.mOutterBall.setColorsAndPosition(OUT_CIRCLE_COLOR_RANGE9, OUT_CIRCLE_POSITION_RANGE);
                break;
        }
        invalidate();
    }

    /* access modifiers changed from: protected */
    public void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        InnerBall innerBall = this.mInnerBall;
        if (innerBall != null) {
            innerBall.draw(canvas);
        }
        OutterBall outterBall = this.mOutterBall;
        if (outterBall != null) {
            outterBall.draw(canvas);
        }
    }

    public boolean onTouchEvent(MotionEvent motionEvent) {
        if (isRunningOfAnimators()) {
            return false;
        }
        int action = motionEvent.getAction();
        if (action == 0) {
            this.mMotionDownX = motionEvent.getX();
            this.mMotionDownY = motionEvent.getY();
            this.mLastDownTime = System.currentTimeMillis();
            this.mDownMotionX = (int) motionEvent.getRawX();
            this.mDownMotionY = (int) motionEvent.getRawY();
            this.xInView = motionEvent.getX();
            this.yInView = motionEvent.getY();
            if (isScreenPortrait(this.mContext)) {
                this.xInScreen = motionEvent.getRawX();
                this.yInScreen = motionEvent.getRawY() - ((float) getStatusBarHeight());
            } else {
                this.xInScreen = motionEvent.getRawX() - ((float) getStatusBarHeight());
                this.yInScreen = motionEvent.getRawY();
            }
            this.mIslongPress = false;
            this.mMaxMoveDistance = 0.0f;
        } else if (action == 1) {
            if (isClick(motionEvent)) {
                doClickEffect();
            }
            this.isBeingDrag = false;
            startDragAnim(judgeWhichDirection(motionEvent.getX() - ((float) this.center.x), motionEvent.getY() - ((float) this.center.y)));
            resetMaxMoveDistance();
        } else if (action == 2) {
            getMaxMoveDistance(motionEvent);
            int rawX = (int) (motionEvent.getRawX() - ((float) this.mDownMotionX));
            int rawY = (int) (motionEvent.getRawY() - ((float) this.mDownMotionY));
            if (isLongTouch() && !this.mIslongPress) {
                this.mIslongPress = true;
                this.mVibrator.vibrate(this.mPattern, -1);
            }
            if (YYFloatButton.getFloatMode(this.mContext)) {
                if (!this.mIslongPress) {
                    if (isScreenPortrait(this.mContext)) {
                        this.xInScreen = motionEvent.getRawX();
                        this.yInScreen = motionEvent.getRawY() - ((float) getStatusBarHeight());
                    } else {
                        this.xInScreen = motionEvent.getRawX() - ((float) getStatusBarHeight());
                        this.yInScreen = motionEvent.getRawY();
                    }
                    OnLocationChangeListener onLocationChangeListener = this.mOnLocationChangeListener;
                    if (onLocationChangeListener != null) {
                        onLocationChangeListener.result(this.xInScreen, this.xInView, this.yInScreen, this.yInView);
                    }
                } else if (this.isBeingDrag || Math.abs(rawX) > this.mScaleTouchSlop || Math.abs(rawY) > this.mScaleTouchSlop) {
                    this.isBeingDrag = true;
                    this.mDistance = (int) Math.sqrt((double) ((rawX * rawX) + (rawY * rawY)));
                    float x = motionEvent.getX() - ((float) this.center.x);
                    float y = motionEvent.getY() - ((float) this.center.y);
                    this.mNowmode = judgeWhichDirection(x, y);
                    if (YYFloatButton.mOnDirectListenerRightTime != null) {
                        YYFloatButton.mOnDirectListenerRightTime.result(false, this.mNowmode);
                    }
                    float calculateAngle = calculateAngle(x, y);
                    OutterBall outterBall = this.mOutterBall;
                    if (outterBall != null) {
                        outterBall.setAngle(calculateAngle);
                        this.mOutterBall.setOffset(this.mDistance);
                    }
                    InnerBall innerBall = this.mInnerBall;
                    if (innerBall != null) {
                        innerBall.setAngle(calculateAngle);
                        this.mInnerBall.setOffset(this.mDistance);
                    }
                    invalidate();
                }
            } else if (this.mIslongPress) {
                if (isScreenPortrait(this.mContext)) {
                    this.xInScreen = motionEvent.getRawX();
                    this.yInScreen = motionEvent.getRawY() - ((float) getStatusBarHeight());
                } else {
                    this.xInScreen = motionEvent.getRawX() - ((float) getStatusBarHeight());
                    this.yInScreen = motionEvent.getRawY();
                }
                OnLocationChangeListener onLocationChangeListener2 = this.mOnLocationChangeListener;
                if (onLocationChangeListener2 != null) {
                    onLocationChangeListener2.result(this.xInScreen, this.xInView, this.yInScreen, this.yInView);
                }
            } else if (this.isBeingDrag || Math.abs(rawX) > this.mScaleTouchSlop || Math.abs(rawY) > this.mScaleTouchSlop) {
                this.isBeingDrag = true;
                this.mDistance = (int) Math.sqrt((double) ((rawX * rawX) + (rawY * rawY)));
                float x2 = motionEvent.getX() - ((float) this.center.x);
                float y2 = motionEvent.getY() - ((float) this.center.y);
                this.mNowmode = judgeWhichDirection(x2, y2);
                if (YYFloatButton.mOnDirectListenerRightTime != null) {
                    YYFloatButton.mOnDirectListenerRightTime.result(false, this.mNowmode);
                }
                float calculateAngle2 = calculateAngle(x2, y2);
                OutterBall outterBall2 = this.mOutterBall;
                if (outterBall2 != null) {
                    outterBall2.setAngle(calculateAngle2);
                    this.mOutterBall.setOffset(this.mDistance);
                }
                InnerBall innerBall2 = this.mInnerBall;
                if (innerBall2 != null) {
                    innerBall2.setAngle(calculateAngle2);
                    this.mInnerBall.setOffset(this.mDistance);
                }
                invalidate();
            }
        } else if (action == 3) {
            this.mIslongPress = false;
            this.isBeingDrag = false;
        }
        return true;
    }

    public boolean isScreenPortrait(Context context) {
        int rotation = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getRotation();
        return (rotation == 1 || rotation == 3) ? false : true;
    }

    private Mode judgeWhichDirection(float f, float f2) {
        Mode mode = Mode.NONE;
        double sqrt = Math.sqrt((double) ((f * f) + (f2 * f2)));
        float min = (float) (Math.min(getMeasuredWidth(), getMeasuredHeight()) / 2);
        if (sqrt >= ((double) (this.mUpThreshold * min)) && sqrt <= ((double) (min * this.mDownThreshold))) {
            return mode;
        }
        if (Math.abs(f) > Math.abs(f2)) {
            if (f > 0.0f) {
                return Mode.RIGHT;
            }
            return Mode.LEFT;
        } else if (f2 > 0.0f) {
            return Mode.DOWN;
        } else {
            return Mode.UP;
        }
    }

    private float calculateAngle(float f, float f2) {
        if (f == 0.0f) {
            return 0.0f;
        }
        float atan = (float) ((Math.atan((double) (f2 / f)) * 180.0d) / 3.141592653589793d);
        return f < 0.0f ? atan + 180.0f : atan;
    }

    private void startDragAnim(final Mode mode) {
        if (!isRunningOfAnimators()) {
            ValueAnimator ofInt = ValueAnimator.ofInt(this.mDistance, 0);
            this.mDragAnimator = ofInt;
            ofInt.setIntValues(this.mDistance, 0);
            this.mDragAnimator.setDuration(400L);
            this.mDragAnimator.setInterpolator(new OvershootInterpolator());
            this.mDragAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                /* class com.lmiot.floatviewsdklibrary.View.FloatView.AnonymousClass1 */

                public void onAnimationUpdate(ValueAnimator valueAnimator) {
                    FloatView.this.mDistance = ((Integer) valueAnimator.getAnimatedValue()).intValue();
                    if (FloatView.this.mOutterBall != null) {
                        FloatView.this.mOutterBall.setOffset(FloatView.this.mDistance);
                    }
                    if (FloatView.this.mInnerBall != null) {
                        FloatView.this.mInnerBall.setOffset(FloatView.this.mDistance);
                    }
                    FloatView.this.invalidate();
                }
            });
            this.mDragAnimator.addListener(new AnimatorListenerAdapter() {
                /* class com.lmiot.floatviewsdklibrary.View.FloatView.AnonymousClass2 */

                public void onAnimationEnd(Animator animator) {
                    super.onAnimationEnd(animator);
                    if (YYFloatButton.getFloatMode(FloatView.this.mContext)) {
                        if (FloatView.this.mIslongPress) {
                            FloatView.this.doFunction(mode);
                        }
                    } else if (!FloatView.this.mIslongPress) {
                        FloatView.this.doFunction(mode);
                    }
                    FloatView.this.mIslongPress = false;
                }
            });
            this.mDragAnimator.start();
        }
    }

    public void startClickAnimator() {
        if (!isRunningOfAnimators()) {
            float min = (float) (Math.min(getMeasuredWidth(), getMeasuredHeight()) / 2);
            float f = this.mInnerRadiusRate;
            ValueAnimator ofFloat = ValueAnimator.ofFloat(min * f, min * f * 1.2f, min * f);
            this.mClickAnimator = ofFloat;
            if (!ofFloat.isRunning()) {
                this.mClickAnimator.setDuration(500L);
                this.mClickAnimator.setInterpolator(new AccelerateDecelerateInterpolator());
                this.mClickAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                    /* class com.lmiot.floatviewsdklibrary.View.FloatView.AnonymousClass3 */

                    public void onAnimationUpdate(ValueAnimator valueAnimator) {
                        float floatValue = ((Float) valueAnimator.getAnimatedValue()).floatValue();
                        if (FloatView.this.mInnerBall != null) {
                            FloatView.this.mInnerBall.setAllRadius((int) floatValue);
                        }
                        FloatView.this.invalidate();
                    }
                });
                this.mClickAnimator.addListener(new AnimatorListenerAdapter() {
                    /* class com.lmiot.floatviewsdklibrary.View.FloatView.AnonymousClass4 */

                    public void onAnimationEnd(Animator animator) {
                        super.onAnimationEnd(animator);
                        FloatView.this.doFunction(Mode.CLICK);
                    }
                });
                this.mClickAnimator.start();
            }
        }
    }

    private boolean isRunningOfAnimators() {
        ValueAnimator valueAnimator = this.mDragAnimator;
        if (valueAnimator != null && valueAnimator.isRunning()) {
            return true;
        }
        ValueAnimator valueAnimator2 = this.mClickAnimator;
        if (valueAnimator2 == null || !valueAnimator2.isRunning()) {
            return false;
        }
        return true;
    }


    private void doFunction(Mode mode) {
        int i = mode.ordinal();
        if (i == 1) {
            callBack(Mode.CLICK);
            callBackRightTime(true, Mode.CLICK);
        } else if (i == 2) {
            callBack(Mode.LEFT);
            callBackRightTime(true, Mode.LEFT);
        } else if (i == 3) {
            callBack(Mode.UP);
            callBackRightTime(true, Mode.UP);
        } else if (i == 4) {
            callBack(Mode.RIGHT);
            callBackRightTime(true, Mode.RIGHT);
        } else if (i == 5) {
            callBack(Mode.DOWN);
            callBackRightTime(true, Mode.DOWN);
        }
    }

    private void callBack(Mode mode) {
        if (YYFloatButton.mOnDirectListener != null) {
            YYFloatButton.mOnDirectListener.result(mode);
        }
    }

    private void callBackRightTime(boolean z, Mode mode) {
        if (YYFloatButton.mOnDirectListenerRightTime != null) {
            YYFloatButton.mOnDirectListenerRightTime.result(z, mode);
        }
    }

    private boolean isLongTouch() {
        return this.mMaxMoveDistance < ((float) this.mScaleTouchSlop) && System.currentTimeMillis() - this.mLastDownTime >= LONG_CLICK_LIMIT;
    }

    private void getMaxMoveDistance(MotionEvent motionEvent) {
        this.mMaxMoveDistance = Math.max(this.mMaxMoveDistance, Math.max(Math.abs(motionEvent.getX() - this.mMotionDownX), Math.abs(motionEvent.getY() - this.mMotionDownY)));
    }

    private void resetMaxMoveDistance() {
        this.mMaxMoveDistance = 0.0f;
    }

    private boolean isClick(MotionEvent motionEvent) {
        return this.mMaxMoveDistance < ((float) this.mScaleTouchSlop);
    }

    private void doClickEffect() {
        startClickAnimator();
    }

    private int getStatusBarHeight() {
        return StateBarUtil.getStatusBarHeight(this.mContext);
    }
}
